﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUL;
using Public;

namespace Presentation
{
    public partial class frmCreatePUnit : Form
    {

        UnitBUL proUnit = new UnitBUL();
        int selectedUnitId;
        public frmCreatePUnit()
        {
            InitializeComponent();
        }

        private void frmCreatePUnit_Load(object sender, EventArgs e)
        {
            searchProUnit("");
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string searchValue = txtSearch.Text.Trim();
            searchProUnit(searchValue);
        }

        public void searchProUnit(string searchValue)
        {
            dgvProUnit.DataSource = proUnit.searchProUnit(searchValue);
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            Unit up = new Unit();
            up.UnitName = txtProUnit.Text.Trim();
            proUnit.AddProductUnit(up);
            frmCreatePUnit_Load(sender, e);
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            Unit up = new Unit();
            up.UnitName = txtProUnit.Text.Trim();
            up.UnitId = this.selectedUnitId;
            proUnit.UpdateProductUnit(up);
            frmCreatePUnit_Load(sender, e);
        }

        private void selectedItem(object sender, DataGridViewCellEventArgs e)
        {
            int index = dgvProUnit.CurrentRow.Index;
            string unitId = dgvProUnit.Rows[index].Cells[0].Value.ToString();
            this.selectedUnitId = int.Parse(unitId);
            this.txtProUnit.Text = dgvProUnit.Rows[index].Cells[1].Value.ToString();
        }

        private void frmCreatePUnit_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult rs = MessageBox.Show("Are you really want to exist?", "Confirm", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (rs == DialogResult.OK)
            {
                frmManageBatch manageBatch = new frmManageBatch();
                this.Hide();
                manageBatch.Show();
            }
            else
            {
                e.Cancel = true;
            }
        }
    }
}
